package com.demo.controller;


import com.demo.util.BaiDuFaceUtil;
import com.demo.result.ApiResult;
import com.demo.service.FaceUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yl
 * @since 2020-11-17
 */
@RestController
public class FaceUserController {

    @Autowired
    BaiDuFaceUtil baiDuFaceUtil;


    @Autowired
    private FaceUserService userService;

    /**
     * 测试数据库是否连上
     *
     * @param id
     * @return
     */
    @RequestMapping("/getUser")
    public ApiResult getUser(int id) {
        return ApiResult.ok(userService.getById(id));
    }


    /**
     * 注册
     *
     * @param request
     * @return
     */
    @RequestMapping("/register")
    public ApiResult FaceRegister(HttpServletRequest request) {
        System.out.println("---------faceregister-------");
        String image = request.getParameter("base");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        System.out.println(image);
        String detect = baiDuFaceUtil.detect(image);
        System.out.println("<============检测人脸信息========>" + detect);
        return userService.add(image, name, password);

    }

//    @RequestMapping("/login/searchface")
//    @ResponseBody
//    public String searchface(@RequestBody @RequestParam(name = "imagebast64") StringBuffer imagebast64, Model model, HttpServletRequest request) throws IOException {
//        Map<String, Object> searchface = faceUtil.search(imagebast64);
//        if(searchface==null||searchface.get("user_id")==null){
//            System.out.println("我进来了");
//            String flag="fail";
//            String s = GsonUtils.toJson(flag);
//            return s;
//        }
//        String user_id = searchface.get("user_id").toString();
//        String score=searchface.get("score").toString().substring(0,2);
//        int i = Integer.parseInt(score);
//        if(i>80){
//            model.addAttribute("userinf",user_id);
//            HttpSession session = request.getSession();
//            session.setAttribute("userinf",user_id);
//            System.out.println("存入session");
//        }
//
//
//        System.out.println(searchface);
//        String s = GsonUtils.toJson(searchface);
//        return s;
//
//
//    }
}
